package com.slf.engine.services.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.jsp.tagext.Tag;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

import com.slf.engine.bo.Order;
import com.slf.engine.common.SysContants;
import com.slf.engine.exception.EngineException;
import com.slf.engine.http.HttpClientManager;
import com.slf.engine.http.HttpResponseWrapper;
import com.slf.engine.services.IHfzcService;
import com.slf.engine.utils.DateUtils;
import com.slf.engine.utils.KeyedDigestMD5;

public class HfzcServiceImpl implements IHfzcService {

	private static final Logger log = Logger.getLogger(HfzcServiceImpl.class);
	
	public Order doPay(Order order)
	{
		StringBuffer url = new StringBuffer();
		url.append("agentid=" + SysContants.AGENTID);
		url.append("&orderid=" + order.getOrderId());
		url.append("&money=" + order.getMoney());
		url.append("&telephonetype=" + SysContants.TYPE);
		if(order.getAccount().startsWith("0"))
		{
			if(order.getAccount().startsWith("010") || order.getAccount().startsWith("021") || order.getAccount().startsWith("022") || order.getAccount().startsWith("023"))
			{
				url.append("&mobilenum=" + order.getAccount().substring(0, 3) + "-" + order.getAccount().substring(3, order.getAccount().length()));
			}else{
				url.append("&mobilenum=" + order.getAccount());
			}
		}else{
			url.append("&mobilenum=" + order.getAccount());
		}
		url.append("&verifyString=" + KeyedDigestMD5.getKeyedDigest(url.toString() + "&merchantKey=" + SysContants.MERCHANTKEY, ""));
		
		HttpClientManager manager = new HttpClientManager();
		
		try {
			log.log(Priority.INFO, "充值提交：" + url.toString());
			HttpResponseWrapper wrapper = manager.execute("GET", SysContants.PAY_URL + "?" + url.toString(), SysContants.DEFAULT_CHARSET, null, null, null, null,null);
			if(wrapper.getBody() != null)
			{
				String body = new String(wrapper.getBody(),SysContants.DEFAULT_CHARSET);
				log.log(Priority.INFO, "充值返回：" + body);
				if("008".equals(body) || "011".equals(body))
				{
					order.setClbz(SysContants.CZ_RUN);
				}else{
					order.setClbz(SysContants.CZ_FAILURE);
				}
			}else{
				return doPay(order);
			}
		} catch (EngineException e) {
			// TODO Auto-generated catch block
			return doPay(order);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
		}
		return order;
	}
	
	public Order doQuery(Order order)
	{
		StringBuffer url = new StringBuffer();
		url.append("agentid=" + SysContants.AGENTID);
		url.append("&orderid=" + order.getOrderId());
		url.append("&verifyString=" + KeyedDigestMD5.getKeyedDigest(url.toString()+ "&merchantKey=" + SysContants.MERCHANTKEY, ""));
		HttpClientManager manager = new HttpClientManager();
		try {
			log.log(Priority.INFO, "查询提交:" + url.toString());
			HttpResponseWrapper wrapper = manager.execute("GET", SysContants.QUERY_URL + "?" + url.toString(), SysContants.DEFAULT_CHARSET, null, null, null,null, null);
			if(wrapper.getBody() != null)
			{
				String body = new String(wrapper.getBody());
				
				log.log(Priority.INFO, "查询返回:" + body);
				if("006".equals(body))
				{
					order.setClbz(SysContants.CZ_RUN);
				}else if("008".equals(body)){
					order.setClbz(SysContants.CZ_SUCCESS);
					order.setCljg("充值成功");
				}else if("088".equals(body)){
					order.setClbz(SysContants.CZ_FAILURE);
					order.setCljg("部分到帐");
				}else if("007".equals(body)){
					order.setCljg("充值失败");
					order.setClbz(SysContants.CZ_FAILURE);
				}else{
					order.setCljg("充值异常:"+body);
					order.setClbz(SysContants.CZ_UNKNOW);
				}
			}else{
				return doQuery(order);
			}
		} catch (EngineException e) {
			// TODO Auto-generated catch block
			return doQuery(order);
		}
		return order;
	}
	
	public static void main(String args[])
	{
		Order order = new Order();
		order.setAccount("18627777692");
		order.setMoney("50");
		order.setOrderId("12312311");
		
		new HfzcServiceImpl().doPay(order);
	}
}
